<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="事件名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入事件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:sonsorEvent:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sonsorEventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事件名称" align="center" prop="name" />
      <el-table-column label="事件编号" align="center" prop="eventCode" />
      <el-table-column label="优先级" align="center" prop="priority" />
      <el-table-column label="传感器" align="center" prop="sensorId" >
        <template slot-scope="scope">
          {{ scope.row.seriesName + '->' + scope.row.sensorName }}
        </template>
      </el-table-column>
      <el-table-column label="表达式" align="center" prop="expression" />
      <el-table-column label="触发方式" align="center" prop="triggerModel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sensor_trigger_model" :value="scope.row.triggerModel"/>
        </template>
      </el-table-column>
      <el-table-column label="通知方式" align="center" prop="alarmModel" width="300">
          <template slot-scope="scope">
            <el-checkbox
              v-for="dict in dict.type.notify_model"
              :key="dict.value"
              :label="parseInt(dict.value)"
              :checked="scope.row.alarmModel?scope.row.alarmModel.indexOf(dict.value) >=0 : false"
              :disabled="true"
            >{{dict.label}}</el-checkbox>
          </template>
      </el-table-column>
      <el-table-column label="报警内容" align="center" prop="alarmContent" />
      <el-table-column label="报警次数" align="center" prop="alarmCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:sonsorEvent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="sendToDevice(scope.row)"
            v-hasPermi="['device:sonsorEvent:edit']"
          >下发</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:sonsorEvent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改事件定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="事件名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入事件名称" />
        </el-form-item>
        <el-form-item label="事件编号" prop="eventCode">
          <el-input v-model="form.eventCode" placeholder="请输入事件编号" />
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-input-number type="number" v-model="form.priority" placeholder="请输入优先级" :min="1" :max="10" />
        </el-form-item>
        <!-- <el-form-item label="所属网关" prop="seriesId">
          <el-select v-model="form.seriesId" placeholder="请选择网关" @change="getSersonList">
            <el-option v-for="item in seriesList" :key="item.id" :value="item.id" :label="item.name"/>
          </el-select>
        </el-form-item> -->
        <el-form-item label="所属传感器" prop="sensorId" :disabled="!form.seriesId">
          <el-select v-model="form.sensorId" placeholder="请选传感器">
            <el-option v-for="item in sensorList" :key="item.id" :value="item.id" :label="item.name"/>
          </el-select>
        </el-form-item>
        <el-form-item label="表达式" prop="expression">
          <el-input v-model="form.expression" placeholder="请输入表达式" />
        </el-form-item>
        <el-form-item label="触发方式">
          <el-radio-group v-model="form.triggerModel">
            <el-radio
              v-for="dict in dict.type.sensor_trigger_model"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="告警方式">
          <el-checkbox-group v-model="form.alarmModel">
            <el-checkbox
              v-for="dict in dict.type.notify_model"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="报警内容" prop="alarmContent">
          <el-input v-model="form.alarmContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSonsorEvent, getSonsorEvent, delSonsorEvent, addSonsorEvent, updateSonsorEvent,sendToDevice } from "@/api/device/sonsorEvent";
import { optionSeries } from "@/api/device/series";
import { seriesIoOptions } from "../../../api/device/seriesIo";
export default {
  name: "SonsorEvent",
  dicts: ['notify_model', 'sensor_trigger_model'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 事件定义表格数据
      sonsorEventList: [],
      sensorList:[],
      seriesList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        deviceId:0
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "事件名称不能为空", trigger: "blur" }
        ],
        seriesId: [
          { required: true, message: "所属系列不能为空", trigger: "blur" }
        ],
        sensorId: [
          { required: true, message: "所属传感器不能为空", trigger: "blur" }
        ],
        eventCode: [
          { required: true, message: "事件编号不能为空", trigger: "blur" }
        ],
        priority: [
          { required: true, message: "优先级不能为空", trigger: "blur" }
        ],
        expression: [
          { required: true, message: "表达式不能为空", trigger: "blur" }
        ],
        alarmModel: [
          { required: true, message: "告警方式不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "是否有效不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getSersonList();
  },
  methods: {
    /** 查询事件定义列表 */
    getList() {
      this.loading = true;
      listSonsorEvent(this.queryParams).then(response => {
        this.sonsorEventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询网关
    getSeriesList(){
      optionSeries().then(res => this.seriesList = res.data)
    },
    //查询传感器下拉列表
    getSersonList(){
      seriesIoOptions().then( res => this.sensorList = res.data);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        seriesId: null,
        sensorId: null,
        name: null,
        eventCode: null,
        priority: null,
        expression: null,
        triggerModel: "device_upload",
        alarmModel: [],
        alarmContent: null,
        state: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加事件定义";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSonsorEvent(id).then(response => {
        this.form = response.data;
        this.form.alarmModel = this.form.alarmModel.split(",");
        this.open = true;
        this.title = "修改事件定义";
      });
    },
    sendToDevice(row){
      sendToDevice(row.id).then(res => this.$modal.msgSuccess("操作成功!"))
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.alarmModel = this.form.alarmModel.join(",");
          if (this.form.id != null) {
            updateSonsorEvent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSonsorEvent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除事件定义编号为"' + ids + '"的数据项？').then(function() {
        return delSonsorEvent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/sonsorEvent/export', {
        ...this.queryParams
      }, `sonsorEvent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
